package com.bj58.disconf.web.service.user;

import java.util.List;

import com.bj58.disconf.web.common.utils.DBHelper;
import com.bj58.disconf.web.service.user.bo.User;
import com.google.inject.Singleton;

/**
 * @author lemon
 * @version 2013-11-28
 */
@Singleton
public class UserDaoImpl {

    public Long insert(User user) {
        try {
            Object id = DBHelper.getInstance().getDao().sql.insert(user);
            if (id != null) {
                return (Long) id;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0l;
    }

    public void update(User user) {
        try {
            DBHelper.getInstance().getDao().sql.upateEntity(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 执行SQL
     * 
     */
    public void executeSql(String sql) {
        try {
            DBHelper.getInstance().getDao().sql.execBySQL(sql, "");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 
     */
    @SuppressWarnings("unchecked")
    public User getUserByName(String name) {
        try {
            List<User> ulist = DBHelper.getInstance().getDao().sql.getListByCustom(User.class, "*", "name='" + name + "'", "");
            return ulist != null ? ulist.get(0) : null;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public User getUserByID(long id) {
        try {
            List<User> ulist = DBHelper.getInstance().getDao().sql.getListByIDS(User.class, new Object[] { id });
            return ulist != null ? ulist.get(0) : null;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    public List<User> findAll() {
        try {
            List<User> ulist = DBHelper.getInstance().getDao().sql.getListByCustom(User.class, "*", "", "");
            return ulist;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}